package zuosheng.stackandQueue;

import java.util.Stack;

/**
 * @author lj
 * @Description: 由两个栈组成的队列
 * @date 2020-11-14 13:03
 * @QQ 851477108
 */


public class DoubleStackQueue {

    public Stack<Integer> stackPush;
    public Stack<Integer> stackPop;

    public DoubleStackQueue(){
        stackPush = new Stack<>();
        stackPop = new Stack<>();
    }

    //push栈向pop栈倒入数据
    private void pushToPop(){
        //条件一：必须保证一次全部导入
        if (stackPop.empty()){
            while (!stackPush.empty()){
                stackPop.push(stackPush.pop());
            }
        }
    }

    public void add(int pushInt){
        stackPush.push(pushInt);
        pushToPop();
    }

    public int pop(){
        if (stackPop.empty() && stackPush.empty()){
            throw new RuntimeException("Queue is empty!");
        }
        pushToPop();
        return stackPop.pop();
    }

    public int peek(){
        if (stackPop.empty() && stackPush.empty()){
            throw new RuntimeException("Queue is empty!");
        }
        pushToPop();
        return stackPop.peek();
    }
}
